//
//  DisCoverYouJIManager.m
//  赵彬旭
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 赵彬旭. All rights reserved.
//

#import "DisCoverYouJIManager.h"
#import "DisCoverYouJIModel.h"

@interface DisCoverYouJIManager ()



@end

@implementation DisCoverYouJIManager

static DisCoverYouJIManager *manager = nil;

+(DisCoverYouJIManager *)shareDisCoverYouJIManager{

    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken,^{
    
        manager = [[DisCoverYouJIManager alloc] init];
   
    });
    
    return manager;
    
}


-(void)requestNetWork:(NSString *)sender{  //游记解析

    NSURL *url = [NSURL URLWithString:sender];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    self.dataArray = [NSMutableArray array];
    
    self.sourceArray = [NSMutableArray array];
    
    self.numberArray = [NSMutableArray array];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data != nil) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            self.sourceArray = dict[@"data"][@"elements"];

            
                for (int i =0; i<_sourceArray.count; i++) {
                    
                    if (_sourceArray[i][@"type"] == _sourceArray[2][@"type"]) {
                        
                        [_numberArray addObject:_sourceArray[i]];
                        
                    }
                    
                }
            
            }
        
            for (NSMutableDictionary *dictionary in _numberArray) {
                
                DisCoverYouJIModel *model = [[DisCoverYouJIModel alloc] init];
                
                [model setValuesForKeysWithDictionary:dictionary];
                
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.upDataUI();
            });
     
    }];

    [task resume];


}





-(NSMutableArray *)dataArray{

    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }

    return _dataArray;
}



-(void)requestXuanShangNetWork:(NSString *)sender{ //悬赏

    
    
    NSURL *url = [NSURL URLWithString:sender];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    self.xuanshangDataArray = [NSMutableArray array];
    
    self.xuanshangSourceArray = [NSMutableArray array];
    
    self.xuanshangNumberArray = [NSMutableArray array];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data != nil) {
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            self.xuanshangSourceArray = dict[@"data"][@"trends_list"];
            
           // NSLog(@"%ld",_xuanshangSourceArray.count);
            
            
            
            for (int i =0; i<_xuanshangSourceArray.count; i++) {
                
                if ([_xuanshangSourceArray[i][@"type"]isEqualToString:@"trends"]) {
                    
                    [_xuanshangNumberArray addObject:_xuanshangSourceArray[i]];
                    
                }
                
            }
            
    
        
        
        
        
        for (NSMutableDictionary *dictionary in _xuanshangNumberArray) {
            
            DisCoverYouJIModel *model = [[DisCoverYouJIModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dictionary];
            
            [self.xuanshangDataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.xuanshangUpDataUI();
        });
            
        }
        
    }];
    
    [task resume];

}










@end
